package org.truenewx.tnxjee.webmvc.api.swagger;

import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.truenewx.tnxjee.core.config.AppConstants;
import org.truenewx.tnxjee.core.version.VersionReader;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;

/**
 * Swagger配置器支持（Springdoc OpenAPI）
 */
public abstract class SwaggerConfigurerSupport {

    @Autowired
    private VersionReader versionReader;
    @Value(AppConstants.EL_SPRING_APP_NAME)
    private String appName;

    @Bean
    public GroupedOpenApi groupedOpenApi() {
        return GroupedOpenApi.builder()
                .group(this.appName)
                .packagesToScan(getBasePackage())
                .pathsToMatch("/**")
                .build();
    }

    @Bean
    public OpenAPI openAPI() {
        String version = this.versionReader.getVersion().toString();
        return new OpenAPI().info(new Info().version(version));
    }

    protected abstract String getBasePackage();

}
